﻿/* ***************************************************************************************
 * Linq To SystemSearch
 * 
 *
 * This is an example of how extend LinQ using IQueruable and IOrderedQuerable interfaces.
 * 
 * User this code as you want,  is free.
 * 
 * Develop by: Mario Rodriguez
 * email: marior666@hotmail.com
 * 
 ********************************************************************************************/

using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;


namespace System.Search.Linq
{
    public class SystemIndex<T> : IOrderedQueryable, IEnumerable<T>
    {
        List<T> elements = new List<T>();
        SystemIndexContext context = new SystemIndexContext();

        public SystemIndexContext Context
        {
            get { 
                return context; 
            }
            set { context = value; }
        }

        public string MachineName { get; set; }

        public IEnumerator<T> Items {

            get {
                foreach (T element in elements) {
                    yield return element;
                }
              }
            }
        #region IEnumerable<T> Members

        public IEnumerator<T> GetEnumerator()
        {
            return Items;
        }

        #endregion

        #region IEnumerable Members

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return Items as System.Collections.IEnumerator;
        }

        #endregion


        public SystemIndex() { }

        public SystemIndex(string machine_name) {

            this.MachineName= machine_name;
        }

        #region IQueryable Members

        public Type ElementType
        {
            get { return typeof (T); }
        }

        public Expression Expression
        {
            get { return Expression.Constant(this); }
        }

        public IQueryProvider Provider
        {
            get { return new SystemIndexQueryProvider<T>(context); }
        }

        #endregion

        

       



    }
}
